package com.data.practice.array;

import org.junit.Test;

public class Main {
    /**
    *@Description:字符串匹配
    */
    @Test
    public void test_1(){
       String str1 = "韩福贵毕业于江苏理工学院,江苏是一个特别好的地方";
       String str2 = "江苏省";
       //str1是否包含str2
        System.out.println(str1.indexOf(str2));
        if (str1.indexOf(str2)>=0){
            System.out.println("包含");
        }else{
            System.out.println("不包含");
        }
        //---2
       //StringBuffer stringBuffer = new StringBuffer(str1);
       //暴力匹配
       char [] chars1 = str1.toCharArray();
       char [] chars2 = str2.toCharArray();
       int j=0;
        for (int i = 0; i < chars2.length; i++) {
            for (int k = 0; k < chars1.length; k++) {
                    if (chars1[i]==chars2[k]){

                    }
            }
        }
    }
    //字符串替换
    @Test
    public void test2(){
        String str = "Java,Java, hello,world!";
        String newStr = str.replaceAll("Java", "尚硅谷~"); //算法
        System.out.println("newStr=" + newStr);
    }

    //二维数组转化稀疏数组
    @Test
    public void test3(){
        //创建11*11的二维数组
        int[][] arr = new int[11][11];
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                  arr[i][j]=0;
            }
        }
        arr[1][1]=1;arr[2][2]=1;arr[3][3]=2;arr[4][4]=1;
        //输出原始的二维数组
//        for (int []rows:arr){
//            for (int data:rows){
//                System.out.println(data);
//            }
//            System.out.println("\n");
//        }
       //
//        int[] ints = new int[3];
//        ints[0]=1;ints[1]=2;
//        ints[2]=3;
//        for (int data:ints){
//            System.out.println(data);
//        }

        //将二维数组转化为稀疏数组
        int sum=0;
        for (int i = 0; i <arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                   if (arr[i][j]!=0)
                        sum++;
            }
        }
        //根据所得到的实际数据数-创建稀疏数组
        int [][] arrData = new int[sum+1][3];
        arrData[0][0] =11; arrData[0][1] =11; arrData[0][2] =sum;
        int count=0;
        //遍历二维数组--把数据数据放入稀疏数组
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                  if (arr[i][j]!=0){
                      count++;
                      arrData[count][0]=i;
                      arrData[count][1]=j;
                      arrData[count][2]=arr[i][j];
                  }
            }
        }
        //全部转化为了稀疏数组
        //输出稀疏数组
        for (int []rows:arrData){
            for (int data:rows){
                System.out.print(data+" ");
            }
            System.out.println();
        }


    }
///////////////////////////////////
}
